import json
import sys

import pymysql
import requests
import yaml

from Test_Auto import SAAS_login


class Function_Help(object):

    def get_yaml_info(self):
        # 打开YAML文件
        if sys.platform.lower().startswith('win'):
            with open('D:/wangyu-project/SAAS_test_SHYD/Test_Auto/base_info.yaml', 'r') as file:
                # 加载YAML数据到字典对象
                data = yaml.safe_load(file)
                return data
        elif sys.platform.lower().startswith('linux'):
            with open('/test/Test_Auto/base_info.yaml', 'r') as file:
                # 加载YAML数据到字典对象
                data = yaml.safe_load(file)
                return data

    def conn_mysql(self, sql, connect_info):
        connect_info['cursorclass'] = pymysql.cursors.DictCursor
        connect = pymysql.connect(**connect_info)
        cursor = connect.cursor()
        try:
            if sql.strip().lower().startswith('select'):
                cursor.execute(sql)
                result = cursor.fetchall()
                return result
            elif sql.strip().lower().startswith('update'):
                cursor.execute(sql)
                connect.commit()
                return 'success'
            elif sql.strip().lower().startswith('insert'):
                cursor.execute(sql)
                connect.commit()
                return 'success'
            else:
                raise ValueError("目前只支持select update insert")
        except ValueError as e:
            print(f"注意: {e}")
        except Exception as e:
            print(f"发生异常: {e}")
        finally:
            cursor.close()
            connect.close()

    def invoke_interface(self, method, url, data=None):
        headers = SAAS_login.Login().user_login()
        if method == 'get':
            response = requests.get(url=url, headers=headers)
            result = json.loads(response.text)
            return result
        elif method == 'post':
            response = requests.post(url=url, headers=headers, data=json.dumps(data))
            result = json.loads(response.text)
            return result


if __name__ == '__main__':
    test = Function_Help()
    result0 = test.get_yaml_info()
    print(result0)
